<?php
require('common.php');

// return an HTML form for updating the function in this row of Definitions
function update_form($row)
{
	global $user;

	$id = $row['ID'];
	
	$html = "<p><form action=\"edit_domain.php\" method=\"get\">\n";
	$html .= '<input type="hidden" name="ID" value="' . $id . '" />' . "\n";
	$html .= '<input type="hidden" name="Fxn_Call" value="' . $row['Fxn_Call'] . '" />' . "\n";
	$html .= "<table>\n";
	$html .= '<tr><td>Script\'s Name</td><td><input name="English_Name" value="' . $row['English_Name'] . '" /></td></tr>' . "\n";
	// if this is called when a form is submitted, it shouldn't have arguments or a return value.
	$called_from = sql_exec("SELECT * FROM Instances WHERE Fxn_Call='form' AND Var1='$id' ");
	if (mysql_num_rows($called_from) === 0)
	{
		$html .= '<tr><td>Parameter 1</td><td><input name="Var1" value="' . $row['Var1'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Parameter 2</td><td><input name="Var2" value="' . $row['Var2'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Parameter 3</td><td><input name="Var3" value="' . $row['Var3'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Parameter 4</td><td><input name="Var4" value="' . $row['Var4'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Parameter 5</td><td><input name="Var5" value="' . $row['Var5'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Parameter 6</td><td><input name="Var6" value="' . $row['Var6'] . '" /></td></tr>' . "\n";
		$html .= '<tr><td>Return Value</td><td><input name="Returns_To" value="' . $row['Returns_To'] . '" /></td></tr>' . "\n";
	}
	$html .= '<tr><td>Description</td><td><textarea name="Description" rows="5" cols="66" />' . "\n";
	$html .= $row['Description'] . '</textarea></td></tr>' . "\n";
	$html .= '<tr><td><input type="submit" name="save" value="Save" /></td>' . "\n";

	$delete = '<td><input type="submit" name="delete" value="Delete" /></td>';
	
	$html .= $delete . '</tr></table>' . "\n";
	$html .= "</form></p>\n";
		
	return $html;
}


// BEGIN EXECUTION


$all_defs = sql_exec("SELECT * FROM Definitions WHERE User='$user' ORDER BY English_Name ");

// if the form was submitted, store the values
if (!empty($G['save']))
{
	$english_name = $G['English_Name'];
	$domain = $G['Fxn_Call'];
	$desc = $G['Description'];
	$id = $G['ID'];
	
	// later on down we'll store var6 to Var6 in Definitions and so on for all the vars, but if the variable is blank
	// in the form we'll get a null string and if we store that, it'll confuse scripts later on which expect a NULL if 
	// there is nothing for that variable, so instead of storing a null string we have to store NULL
	// be sure to add single quotes to non-NULL values
	$vars_plus_return = $vars;
	$vars_plus_return[] = 'Returns_To';
	foreach ($vars_plus_return as $vr)
	{
		if (empty($G[$vr]))
		{
			$G[$vr] = 'NULL';
		} else {
			$G[$vr] = '\'' . $G[$vr] . '\'';
		}
	}
	
	$var1= $G['Var1'];
	$var2= $G['Var2'];
	$var3= $G['Var3'];
	$var4= $G['Var4'];
	$var5= $G['Var5'];
	$var6= $G['Var6'];
	$returns_to = $G['Returns_To'];
	
	// update Variables 
	// should be done before definition is updated so that we can erase the old variables
	// this is from add_domain.php: "INSERT INTO Variables SET User='$user', Domain='$fxn_call', Name='$var_name'";
	// don't let the user leave a variable NULL if a higher variable is defined?
	
	// erase the old variables
	$def = sql_row1("SELECT * FROM Definitions WHERE ID='$id' ");
	foreach ($vars as $v)
	{
		if (!empty($def[$v]))
		{
			$name = $def[$v];
			sql_exec("DELETE FROM Variables WHERE User='$user' AND Domain='$domain' AND Name='$name' ");
		}
	}
	
	// add the new variables
	foreach ($vars as $v)
	{
		$new_var = $G[$v];
		if (!empty($new_var) && $new_var != 'NULL')
		{
			// remember not to have single quotes for $new_var because we added a pair above because of the 
			// NULL problem in updating Definitions 
			sql_exec("INSERT INTO Variables SET User='$user', Domain='$domain', Name=$new_var ");
		}
	}	
	
	// update the definition
	// the vars and returns_to should have their own single quotes because we want 
	// a true NULL to be stored as Var1=NULL and not Var1='NULL'
	sql_exec("UPDATE Definitions SET 
	English_Name='$english_name', 
	Var1=$var1, 
	Var2=$var2, 
	Var3=$var3, 
	Var4=$var4, 
	Var5=$var5, 
	Var6=$var6, 
	Returns_To=$returns_to, 
	Description='$desc' 
	WHERE ID='$id'");
	
	
	update($user, $domain, $line);
	header("Location: index.php");
}

if (!empty($G['delete']))
{
	$id = $G['ID'];
	$domain = $G['Fxn_Call'];
	// delete the definition and all associated instances 
	sql_exec("DELETE FROM Definitions WHERE ID='$id' ");
	sql_exec("DELETE FROM Instances WHERE User='$user' AND Domain='$domain' ");
	
	// return to All Scripts
	header("Location: index.php");
}

html_header("Edit a Script");
?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<h3>Edit a Script</h3>

<?php
// display a form to edit each definition, returning to this script with a submit button named 'save'
while ($row = mysql_fetch_array($all_defs))
{
	echo update_form($row);
}
?>


<?php echo footer(); ?>

</div>

</div>
</body>
</html>
